package com.sys.controller;

import com.sys.common.JsonResult;
import com.sys.common.O_Search;
import com.sys.common.Pid;
import com.sys.dto.OrderDTO;
import com.sys.dto.ProductDTO;
import com.sys.dto.idto.IOrderDTO;
import com.sys.entity.OrdersEntity;
import com.sys.entity.ProductEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/orders")
public class OrderController extends BaseController{

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<List<OrdersEntity>> findAll(){
        IOrderDTO orderDTO = new OrderDTO();
        List<OrdersEntity> all = orderDTO.findAll();
        System.out.println("所有订单");

        return new JsonResult<List<OrdersEntity>>(EOK, all);
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> reg(OrdersEntity entity){
        IOrderDTO orderDTO = new OrderDTO();
        orderDTO.save(entity);
        return new JsonResult<>(EOK);
    }
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> del(Integer oid, HttpSession session){
        IOrderDTO orderDTO = new OrderDTO();
        orderDTO.delete(oid);
        return new JsonResult<>(EOK);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Void> update(ProductEntity entity){
        Integer pid = entity.getPid();
        ProductDTO productDTO = new ProductDTO();
        productDTO.update(entity,pid);

        System.out.println("修改后的商品是："+entity.getProduceName());
        return new JsonResult<>(EOK);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public JsonResult<Pid> getPid(Integer pid){
        return new JsonResult<Pid>(EOK);
    }

    @RequestMapping(value = "/findsearch", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<List<OrdersEntity>> findsearch(O_Search o_search){
        String ordersId = o_search.getOrdersId();
        String producename = o_search.getProducename();
        String cName = o_search.getcName();
        if (ordersId != null && !ordersId.equals("")){
            System.out.println("根据订单号查询");
            System.out.println("订单号是："+ordersId);
            IOrderDTO orderDTO = new OrderDTO();
            List<OrdersEntity> byProperty = orderDTO.findByProperty("ordersId", ordersId);
            return new JsonResult<List<OrdersEntity>>(EOK, byProperty);
        } else if (producename != null && !producename.equals("")) {
            System.out.println("根据商品名查询");
            System.out.println("商品名是："+producename);
            IOrderDTO orderDTO = new OrderDTO();
            List<OrdersEntity> byProperty = orderDTO.findByProperty("producename", producename);
            return new JsonResult<List<OrdersEntity>>(EOK, byProperty);
        } else if (cName != null && !cName.equals("")) {
            IOrderDTO orderDTO = new OrderDTO();
            System.out.println("根据客户名查询");
            System.out.println("客户名是："+cName);
            List<OrdersEntity> byProperty = orderDTO.findByProperty("cName", cName);
            return new JsonResult<List<OrdersEntity>>(EOK, byProperty);
        }else
            return new JsonResult<List<OrdersEntity>>(EOK);
    }
}


